package com.example.demo.controller;

import cn.hutool.captcha.CaptchaUtil;
import com.example.demo.common.*;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;

/**
 * @author: cheng
 * @date: 2023/6/25 14:26
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private CheckCodeTools codeTools;
    /**
     * 注册
     * @param userInfo
     * @return
     */
    @RequestMapping("/reg")
    public AjaxResult reg(UserInfo userInfo){
        // 1.非空判断
        if(userInfo == null || !StringUtils.hasLength(userInfo.getUsername()) ||
                !StringUtils.hasLength(userInfo.getPassword())){
            return AjaxResult.fail(-1,"参数有误");
        }
        //加盐加密
        userInfo.setPassword(PasswordTools.encrypt(userInfo.getPassword()));
        int result = userService.reg(userInfo);
        return AjaxResult.success(result);
    }

    /**
     * 登录
     * @param username
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public AjaxResult login(String username, String password,String codeinput, HttpServletRequest request){
        // 1.非空判断
        if(!StringUtils.hasLength(username) ||
                !StringUtils.hasLength(password)){
            return AjaxResult.fail(-1,"参数有误");
        }
        UserInfo userInfo = userService.login(username);
        if(userInfo == null || userInfo.getId() <= 0){
            return AjaxResult.fail(-2,"用户名或密码错误");
        }
        if(!PasswordTools.decrypt(password,userInfo.getPassword())){
            return AjaxResult.fail(-2,"用户名或密码错误");
        }
        //验证码校验
        String sc = (String) request.getSession().getAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE);
        if (sc == null || !codeinput.equals(sc)){
            return AjaxResult.fail(-2,"验证码错误");
        }
        // 将用户存储到 session 中
        HttpSession session = request.getSession();

        session.setAttribute(ApplicationVariable.SESSION_KEY_USERINFO,userInfo);
        return AjaxResult.success(1);
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute(ApplicationVariable.SESSION_KEY_USERINFO);
        return AjaxResult.success(1);
    }

    /**
     * 判断是否登录
     * @param request
     * @return
     */
    @RequestMapping("/islogin")
    public AjaxResult isLogin(HttpServletRequest request){
        if(UserSessionTools.getLoginUser(request) == null){
            //已登陆
            return AjaxResult.success(0);
        }
        return AjaxResult.success(1);
    }

    /**
     * 验证码
     * @param request
     * @return
     */
    @RequestMapping("/getcode")
    public AjaxResult getCode(HttpServletRequest request) {
        String[] codeArr = codeTools.createImage();
        // 将验证码存储到 session
        HttpSession session = request.getSession();
        session.setAttribute(ApplicationVariable.SESSION_KEY_CHECK_CODE, codeArr[1]);
        return AjaxResult.success("/image/" + codeArr[0]);
    }
}
